package manager
{
	import data.AccountInfo;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	import loaders.LoaderEvent;
	import loaders.LoaderManager;
	import loaders.LoaderQueue;
	import loaders.SwfLoader;
	
	import mx.messaging.AbstractConsumer;
	
	import utils.StageReferance;
	import utils.StringUtils;

	public final class StartupManager
	{
		public function StartupManager()
		{
		}
		
		private static var _instance:StartupManager;
		public static function get Instance():StartupManager
		{
			if(_instance == null)
			{
				_instance = new StartupManager();
			}
			return _instance;
		}
		
		private var _loaderQueue:LoaderQueue = new LoaderQueue();
		private var _startMode:String;
		public function start(stage:Stage,acc:AccountInfo,mode:String = 'test'):void
		{
			_startMode = mode;
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			
			StageReferance.setup(stage);
			LayerManager.Instance.setup(StageReferance.stage);
			LoginManager.Instance.addEventListener(Event.COMPLETE,onLoginComplete);
			LoginManager.Instance.login(acc);
		}
		
		private function onLoginComplete(event:Event):void
		{
			LoginManager.Instance.removeEventListener(Event.COMPLETE,onLoginComplete);
			loadResource(_startMode);
		}
		
		public function loadResource(mode:String):void
		{
			if(mode=='test')
			{
				var loader:SwfLoader = new SwfLoader("fright/library.swf",null,true);
				_loaderQueue.addLoader(loader);
			}
			_loaderQueue.addEventListener(Event.COMPLETE,onLoadComplete);
			_loaderQueue.addEventListener(LoaderEvent.PROGRESS,onLoadProgress);
			_loaderQueue.start();
		}
		
		protected function onLoadProgress(event:LoaderEvent):void
		{
			trace("正在加载 游戏资源："+StringUtils.getFileNameFromUrl(event.loader.url)+' '+event.loader.progress*100);
		}
		
		protected function onLoadComplete(event:Event):void
		{
			trace("加载完成！！");
			var BattleSceneClass:Class = getDefinitionByName("battle.BattleScene") as Class;
			var battleScene:* = new BattleSceneClass();
			battleScene.runTest();
			LayerManager.Instance.addToLayer(battleScene,LayerManager.GAME_BOTTOM_LAYER);
		}
	}
}